package com.desksoft.ecommerce.util.html.element.data;

import java.util.List;

import org.dom4j.Element;

import com.desksoft.ecommerce.util.html.PageXml;
import com.desksoft.ecommerce.util.html.element.IPageQueryElement;
import com.desksoft.ecommerce.util.html.element.PageDataElement;
import com.desksoft.ecommerce.util.html.element.QueryElement;
import com.desksoft.ecommerce.util.html.element.data.select.Options;
import com.desksoft.ecommerce.util.html.element.data.select.QueryOptions;
import com.desksoft.ecommerce.util.html.element.data.select.StaticOptions;
import com.desksoft.ecommerce.util.html.element.data.select.XmlOptions;
import com.desksoft.ecommerce.vo.KeyAndValue;

/***************************************
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 
 * 创建作者：luoy
 * 文件名称：页面选择输入元素
 * 版本：
 * 功能：
 * 最后修改时间：
 * 修改记录：
 *****************************************/
public class PageSelectDataElement extends PageDataElement implements IPageQueryElement {

	protected Options options;						//	下拉菜单选项
	
	protected String defaultValue;					//	下拉菜单默认值
	
	public PageSelectDataElement(Element element,PageXml pageXml) {
		super(element,pageXml);
		this.pageXml = pageXml;
		this.defaultValue = pageXml.getVarCollection().getValue(element.attributeValue("defaultValue"));
		this.options = parseOptions(element);
		if (this.options instanceof QueryOptions){
			pageXml.getNeedsQueryElement().add(this);
		}
	}

	/**
	 * 解析下拉框中的值
	 */
	public Options parseOptions(Element element){
		Element optionsElmt = element.element("queryOptions");
		Options options = null;
		if (optionsElmt != null){
			options = new QueryOptions(optionsElmt, pageXml);
			return options;
		}
		optionsElmt = element.element("xmlOptions");
		if (optionsElmt != null){
			options = new XmlOptions(optionsElmt);
			return options;
		}
		optionsElmt = element.element("options");
		if (optionsElmt != null){
			options = new StaticOptions(optionsElmt);
			return options;
		}
		return options;
	}
	
	@Override
	public String toHtml() {
		return super.toHtml();
	}
	
	@Override
	public String appendOptions() {
		StringBuffer sbuf = new StringBuffer();
		for (KeyAndValue<String,String> kv:options.getOptions()){
			if (kv.getKey().equals(defaultValue)){
				sbuf.append("<option selected='selected' value="+kv.getKey()+">"+kv.getValue()+"</option>");
			}else{
				sbuf.append("<option value="+kv.getKey()+">"+kv.getValue()+"</option>");
			}
			
		}
		return sbuf.toString();
	}

	@Override
	public String appendAttr() {
		return super.appendAttr();
	}

	
	@Override
	public QueryElement getQueryElement() {
		QueryOptions queryOptions = (QueryOptions) this.options;
		return queryOptions.getQuery();
	}

	@Override
	public void loadData(List list) {
		QueryOptions queryOptions = (QueryOptions) this.options;
		queryOptions.setQueryResult(list);
	}

	public Options getOptions() {
		return options;
	}

	public void setOptions(Options options) {
		this.options = options;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	
	
}
